var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

const adminAuth = require('./middlewares/admin-auth');
const userAuth = require('./middlewares/user-auth');

require('dotenv').config();

// 前台路由文件
var indexRouter = require('./routes/index');
const categoriesRouter = require('./routes/categories');
const coursesRouter = require('./routes/courses');
const chaptersRouter = require('./routes/chapters');
const articlesRouter = require('./routes/articles');
const settingsRouter = require('./routes/settings');
const searchRouter = require('./routes/search')
const authRouter = require('./routes/auth');
var usersRouter = require('./routes/users');
const LikesRouter = require('./routes/likes');


// 后台路由文件
var adminArticlesRouter = require('./routes/admin/articles');
var adminCategoriesRouter = require('./routes/admin/categories');
var adminSettingsRouter = require('./routes/admin/settings');
var adminUsersRouter = require('./routes/admin/users');
var adminCoursesRouter = require('./routes/admin/courses');
var adminChaptersRouter = require('./routes/admin/chapters');
var adminEchartsRouter = require('./routes/admin/echarts');
var adminAuthRouter = require('./routes/admin/auth');


const cors = require('cors');

var app = express();

// 允许所有来源访问（开发环境）
app.use(cors());

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 前台路由配置
app.use('/', indexRouter);
app.use('/categories', categoriesRouter);
app.use('/courses', coursesRouter);
app.use('/chapters', chaptersRouter);
app.use('/articles', articlesRouter);
app.use('/settings', settingsRouter);
app.use('/search', searchRouter);
app.use('/auth', authRouter);
app.use('/users', userAuth, usersRouter); //
app.use('/likes',userAuth, LikesRouter);


// 后台路由配置
app.use('/admin/articles', adminAuth, adminArticlesRouter)
app.use('/admin/categories', adminAuth,  adminCategoriesRouter)
app.use('/admin/settings', adminAuth,  adminSettingsRouter)
app.use('/admin/users', adminAuth,  adminUsersRouter)
app.use('/admin/courses', adminAuth,  adminCoursesRouter)
app.use('/admin/chapters', adminAuth,  adminChaptersRouter)
app.use('/admin/echarts', adminAuth,  adminEchartsRouter)
app.use('/admin/auth', adminAuthRouter)

module.exports = app;
